package com.javasm.sys.controller;

import com.github.pagehelper.PageInfo;
import com.javasm.commons.base.BaseController;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.enums.OperatorAction;
import com.javasm.commons.enums.OperatorType;
import com.javasm.dict.entity.SysDicType;
import com.javasm.dict.service.TypeService;
import com.javasm.sys.annotations.MyLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("type")
public class TypeController extends BaseController {

    @Autowired
    private TypeService ts;

    @GetMapping("allType")
    @MyLog(title = "字典类型模块",operationAction = OperatorAction.OTHER,operatorType = OperatorType.MANAGE)
    public AxiosResult getall(){
        List<SysDicType> all = ts.getAll();
        return AxiosResult.suc(all);
    }

    @GetMapping
    @MyLog(title = "字典类型模块",operationAction = OperatorAction.OTHER,operatorType = OperatorType.MANAGE)
    public AxiosResult gteAllByPage(SysDicType dt){
        startPage();
        List<SysDicType> allByCondition = ts.getAllByCondition(dt);
        PageInfo p = new PageInfo(allByCondition);
        return suc(p);
    }

    @PostMapping
    @MyLog(title = "字典类型模块",operationAction = OperatorAction.OTHER,operatorType = OperatorType.MANAGE)
    public AxiosResult addType(@RequestBody SysDicType dt){
        ts.insetType(dt);
        return suc();
    }

    @PutMapping
    @MyLog(title = "字典类型模块",operationAction = OperatorAction.OTHER,operatorType = OperatorType.MANAGE)
    public AxiosResult updateType(@RequestBody SysDicType dt){
       /* if (dt!=null) throw new MvcException(E.ERROR);*/
        ts.updateType(dt);
        return suc();
    }

    @DeleteMapping
    @MyLog(title = "字典类型模块",operationAction = OperatorAction.OTHER,operatorType = OperatorType.MANAGE)
    public AxiosResult delTypeByIds(String ids){
        String[] split = ids.split(",");
        ts.delByIds(split);
        return suc();
    }


}
